import argparse, numpy as np, healpy as hp, pandas as pd

def healpix_chi2(df, nside=64):
    pix = hp.ang2pix(nside, df['ra'].values, df['dec'].values, lonlat=True)
    counts = np.bincount(pix, minlength=12*nside*nside)
    mu = counts.mean() + 1e-9
    return float(((counts-mu)**2/mu).mean())

if __name__ == "__main__":
    ap = argparse.ArgumentParser()
    ap.add_argument("--check", type=str, required=True)
    ap.add_argument("--nside", type=int, default=64)
    args = ap.parse_args()
    df = pd.read_csv(args.check)
    chi2 = healpix_chi2(df, args.nside)
    print("HEALPix chi2:", chi2)
